package com.zzy.demo.web.download;

import java.io.File;
import java.io.IOException;

import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.zzy.cms.core.util.UUIDGenerator;
import com.zzy.cms.core.util.file.FileCopyUtil;
import com.zzy.demo.common.constant.FileConsant;
import com.zzy.demo.common.util.ExportToUtil;

import lombok.extern.slf4j.Slf4j;

/**
 * 
 * @author guokaige
 *
 */
@Controller
@RequestMapping({ "/downlaod" })
@Slf4j
public class DownloadController {



	@RequestMapping({ "/zip" })
	public void downlaodZip(HttpServletResponse response) {
		//创建导出目录
		String uuid = UUIDGenerator.nextId();
		String sumPath = FileConsant.EXPORT_TEMP_PATH + uuid + File.separator;
		
		//目标目录zip
        File sumPathFile = new File(sumPath);
        if (!sumPathFile.exists()) {
        	boolean flag = sumPathFile.mkdir();
        	if(flag) {
        		log.info("[ \t 创建目录成功， \t {}]", sumPathFile.getPath());
        	}else {
        		log.info("[ \t 创建目录失败， \t {}]", sumPathFile.getPath());
        	}
            
        }
        

        String srcFilePath = FileConsant.MODEL_PATH + "1.txt";
        File srcFile = new File(srcFilePath);
        if (!srcFile.exists()) {
            log.info("[ \t 原文件不存在， \t {}]", srcFilePath );
        }
        
        // 复制文件到目标目录
        FileCopyUtil.copyFileToDirectory(srcFilePath, sumPath);
        
        try {
        	ExportToUtil.responseBodyZip(response, sumPath, "test");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	
	@RequestMapping({ "/excel" })
	public void downlaodExcel(HttpServletResponse response) {
		//创建导出目录
		String filePath = "D://1.xls";
        try {
        	ExportToUtil.exportToFile(response, filePath, "2.xls");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	
	
}
